#include "ComDeviceControl.h"

namespace CommonComponet
{
#define RECONNECT_TIMER 2000
	ComDeviceControl::ComDeviceControl(QObject* parent = NULL)
		: QObject(parent)
	{
		m_running = false;
		m_serial_worker = NULL;
		m_tcp_worker = NULL;
	}

	ComDeviceControl::~ComDeviceControl()
	{
		Stop();
	}

	void ComDeviceControl::Start(const QString& port, const QString& command, DEVICE_CONN_TYPE device_conn_type, int baudRate)
	{
		if (m_running)
			return;

		QSharedPointer<SerialInfo> serical_info = QSharedPointer<SerialInfo>::create();
		serical_info->comPort = port;
		serical_info->baudRate = baudRate;// 115200;
		serical_info->dataBits = 8;
		serical_info->parity = 0;
		serical_info->stopBits = 1;

		m_serial_worker = new SerialPortClient(serical_info, device_conn_type);
		m_serial_worker->moveToThread(&m_workerThread);

		connect(&m_workerThread, &QThread::started, m_serial_worker, &SerialPortClient::Started);
		connect(&m_workerThread, &QThread::finished, m_serial_worker, &SerialPortClient::deleteLater);

		connect(m_serial_worker, &SerialPortClient::sig_connection_state, this, &ComDeviceControl::sig_connection_state);
		connect(m_serial_worker, &SerialPortClient::sig_receiveDataFromServer, this, &ComDeviceControl::sig_receiveDataFromServer);

		connect(this, &ComDeviceControl::sig_Reconnect, m_serial_worker, &SerialPortClient::slot_Reconnect);

		if (device_conn_type == DEVICE_CONN_TYPE::ASYNC_AND_TIME_OUT)
		{
			connect(this, &ComDeviceControl::sig_AsyncAndWaitSendmessage, m_serial_worker, &SerialPortClient::slot_async_timeout_send_message);
		}
		else if (device_conn_type == DEVICE_CONN_TYPE::ASYNC)
		{
			connect(this, &ComDeviceControl::sig_AsyncSendmessage, m_serial_worker, &SerialPortClient::slot_send_message);
		}

		m_command = command;

		m_workerThread.start();
		m_running = true;
	}
	//		QString ip;
	//      int port;
	void ComDeviceControl::StartIpDevice(const QString& ip, int port, const QString& command, DEVICE_CONN_TYPE device_conn_type)
	{
		if (m_running)
			return;

		QSharedPointer<CommonComponet::TcpConnectInfo> tcp_connect_info = QSharedPointer<CommonComponet::TcpConnectInfo>::create();
		tcp_connect_info->ip = ip;
		tcp_connect_info->port = port;

		m_tcp_worker = new TcpClient(tcp_connect_info, device_conn_type, "", 2000, RECONNECT_TIMER);
		m_tcp_worker->moveToThread(&m_workerThread);

		connect(&m_workerThread, &QThread::started, m_tcp_worker, &TcpClient::Started);
		connect(&m_workerThread, &QThread::finished, m_tcp_worker, &TcpClient::deleteLater);

		connect(m_tcp_worker, &TcpClient::sig_connection_state, this, &ComDeviceControl::sig_connection_state);
		connect(m_tcp_worker, &TcpClient::sig_receiveDataFromServer, this, &ComDeviceControl::sig_receiveDataFromServer);

		connect(this, &ComDeviceControl::sig_ReconnectIp, m_tcp_worker, &TcpClient::slot_reconnect_server_by_newconfig);

		if (device_conn_type == DEVICE_CONN_TYPE::ASYNC_AND_TIME_OUT)
		{
			connect(this, &ComDeviceControl::sig_AsyncAndWaitSendmessage, m_tcp_worker, &TcpClient::slot_async_timeout_send_message);
		}
		else if (device_conn_type == DEVICE_CONN_TYPE::ASYNC)
		{
			connect(this, &ComDeviceControl::sig_AsyncSendmessage, m_tcp_worker, &TcpClient::slot_send_message);
		}
		else
		{
			//
		}

		m_command = "T";

		m_workerThread.start();
		m_running = true;
	}

	void ComDeviceControl::Stop()
	{
		if (m_running == true)
		{
			m_running = false;

			m_workerThread.quit();
			m_workerThread.wait();
			qDebug() << "停止";
		}
	}

	void ComDeviceControl::reconnectIp(const QString& ip, int port)
	{
		if (m_tcp_worker)
		{
			emit sig_ReconnectIp(ip, port);
		}
	}

	void ComDeviceControl::reconnect(const QString& com_port, const QString command)
	{
		if (m_serial_worker == NULL)
		{
			return;
		}

		if (command != "")
		{
			m_command = command;
		}

		emit sig_Reconnect(com_port);
	}

	//异步发送。。一直等待返回
	void ComDeviceControl::AsyncSendmessage()
	{
		emit sig_AsyncSendmessage(m_command);
	}

	//异步发送 超时一定返回
	void ComDeviceControl::AsyncAndWaitSendmessage()
	{
		emit sig_AsyncAndWaitSendmessage(m_command, 1000, 1000);
	}

	void ComDeviceControl::AsyncAndWaitSendmessage(int write_timeout, int read_timeout)
	{
		emit sig_AsyncAndWaitSendmessage(m_command, write_timeout, read_timeout);
	}
}
